#define DEBUG
#include <cstdio>

using namespace std;

typedef long long ll;

const int maxn=525010;

class Trie {
	public:
		Trie *son[2];
		int cnt, val, here;

		Trie() {
			son[0] = son[1] = 0;
			cnt = val = here = 0;
		}

		void update() {
			val = 0, cnt = here;
			if (son[0]) {
				val ^= son[0]->val<<1;
				cnt += son[0]->cnt;
			}
			if (son[1]) {
				val ^= son[1]->val<<1;
				val |= son[1]->cnt&1;
				cnt += son[1]->cnt;
			}
		}

		void ins(int x) {
			if (x) {
				if (!son[x&1]) son[x&1] = new Trie();
				son[x&1]->ins(x>>1);
			} else {
				here++;
			}
			update();
		}
		
		void add() {
			Trie *temp;
			temp=son[0], son[0]=son[1], son[1]=temp;
			if (son[0]) son[0]->add();
			if (here) {
				if (!son[1]) son[1] = new Trie();
				son[1]->here += here, here=0;
				son[1]->update();
			}
			update();
		}

		void merge(Trie *o) {
			if (o) {
				here += o->here;
				if (o->son[0]) {
					if (son[0]) son[0]->merge(o->son[0]);
					else son[0] = o->son[0];
				}
				if (o->son[1]) {
					if (son[1]) son[1]->merge(o->son[1]);
					else son[1] = o->son[1];
				}
				update();
			}
		}
};

int main() {
	freopen("tree.in", "r", stdin);
	freopen("tree.out", "w", stdout);

	static Trie *trie[maxn+1];
	static int v[maxn+1], f[maxn+1];
	int n;
	scanf("%d", &n);
	for (int i=1; i<=n; i++) {
		scanf("%d", v+i);
		trie[i] = new Trie();
	}
	for (int i=2; i<=n; i++) scanf("%d", f+i);

	ll ans=0;
	for (int i=n; i>=1; i--) {
		trie[i]->ins(v[i]);
		ans+=trie[i]->val;
		trie[i]->add();
		if (i>1) trie[f[i]]->merge(trie[i]);
	}

	printf("%lld", ans);

	fclose(stdin);
	fclose(stdout);
	return 0;
}
